var MyInputComponent = easyMe.import("public/javascripts/widget/myInput/myInput.js");
var selectFactory = easyMe.import("public/javascripts/widget/select/select.js");
var ordersService = easyMe.import("public/javascripts/service/ordersService.js");
var classNames = easyMe.import("public/common/classNames.js");
var cx = classNames;
var GoodsComponent = React.createClass({
    componentDidMount: function() {
        this.initUnitSelect();
        var self = this;

        if(this.state.info.from === "highVolumeOrders") {

            setTimeout(function () {
                self.checkData();
            },1000)

        }
    },
    checkData:function () {
        var self = this;
        var errorDtoMap = self.state.info.errorDtoMap;
        if(errorDtoMap === null){
            return;
        }

        // 如果物品明细为空也需要把第一条标红
        if(self.state.info.errorDtoMap.cargos){
            self.refs["goodsNameCn_0"].showErrorMsg("");
            self.refs["goodsNameEn_0"].showErrorMsg("");
            self.refs["hsCode_0"].showErrorMsg("");
            self.refs["goodsNumber_0"].showErrorMsg("");
            self.refs["goodsPrice_0"].showErrorMsg("");
            self.refs["description_0"].showErrorMsg("");
        }
        //批量下单，从后台来错误校验 中文名
        if(self.state.info.errorDtoMap.goodsNameCn0) {

            self.refs["goodsNameCn_0"].showErrorMsg(self.state.info.errorDtoMap.goodsNameCn0.message);
        }
        if(self.state.info.errorDtoMap.goodsNameCn1) {

            self.refs["goodsNameCn_1"].showErrorMsg(self.state.info.errorDtoMap.goodsNameCn1.message);
        }

        if(self.state.info.errorDtoMap.goodsNameCn2) {
            self.refs["goodsNameCn_2"].showErrorMsg(self.state.info.errorDtoMap.goodsNameCn2.message);
        }
        if(self.state.info.errorDtoMap.goodsNameCn3) {
            self.refs["goodsNameCn_3"].showErrorMsg(self.state.info.errorDtoMap.goodsNameCn3.message);
        }
        if(self.state.info.errorDtoMap.goodsNameCn4) {
            self.refs["goodsNameCn_4"].showErrorMsg(self.state.info.errorDtoMap.goodsNameCn4.message);
        }
        // 英文名
        if(self.state.info.errorDtoMap.goodsNameEn0) {
            self.refs["goodsNameEn_0"].showErrorMsg(self.state.info.errorDtoMap.goodsNameEn0.message);
        }
        if(self.state.info.errorDtoMap.goodsNameEn1) {
            self.refs["goodsNameEn_1"].showErrorMsg(self.state.info.errorDtoMap.goodsNameEn1.message);
        }
        if(self.state.info.errorDtoMap.goodsNameEn2) {
            self.refs["goodsNameEn_2"].showErrorMsg(self.state.info.errorDtoMap.goodsNameEn2.message);
        }
        if(self.state.info.errorDtoMap.goodsNameEn3) {
            self.refs["goodsNameEn_3"].showErrorMsg(self.state.info.errorDtoMap.goodsNameEn3.message);
        }
        if(self.state.info.errorDtoMap.goodsNameEn4) {
            self.refs["goodsNameEn_4"].showErrorMsg(self.state.info.errorDtoMap.goodsNameEn4.message);
        }
        //hs编码
        if(self.state.info.errorDtoMap.hsCode0) {
            self.refs["hsCode_0"].showErrorMsg(self.state.info.errorDtoMap.hsCode0.message);
        }
        if(self.state.info.errorDtoMap.hsCode1) {
            self.refs["hsCode_1"].showErrorMsg(self.state.info.errorDtoMap.hsCode1.message);
        }
        if(self.state.info.errorDtoMap.hsCode2) {
            self.refs["hsCode_2"].showErrorMsg(self.state.info.errorDtoMap.hsCode2.message);
        }
        if(self.state.info.errorDtoMap.hsCode3) {
            self.refs["hsCode_3"].showErrorMsg(self.state.info.errorDtoMap.hsCode3.message);
        }
        if(self.state.info.errorDtoMap.hsCode4) {
            self.refs["hsCode_4"].showErrorMsg(self.state.info.errorDtoMap.hsCode4.message);
        }

        //数量
        if(self.state.info.errorDtoMap.goodsNumber0) {
            self.refs["goodsNumber_0"].showErrorMsg(self.state.info.errorDtoMap.goodsNumber0.message);
        }
        if(self.state.info.errorDtoMap.goodsNumber1) {
            self.refs["goodsNumber_1"].showErrorMsg(self.state.info.errorDtoMap.goodsNumber1.message);
        }
        if(self.state.info.errorDtoMap.goodsNumber2) {
            self.refs["goodsNumber_2"].showErrorMsg(self.state.info.errorDtoMap.goodsNumber2.message);
        }
        if(self.state.info.errorDtoMap.goodsNumber3) {
            self.refs["goodsNumber_3"].showErrorMsg(self.state.info.errorDtoMap.goodsNumber3.message);
        }
        if(self.state.info.errorDtoMap.goodsNumber4) {
            self.refs["goodsNumber_4"].showErrorMsg(self.state.info.errorDtoMap.goodsNumber4.message);
        }

        //计量单位
        if(self.state.info.errorDtoMap.goodsUnitName0) {
            self.refs["goodsUnitName_0"].showErrorMsg(self.state.info.errorDtoMap.goodsUnitName0.message);
        }
        if(self.state.info.errorDtoMap.goodsUnitName1) {
            self.refs["goodsUnitName_1"].showErrorMsg(self.state.info.errorDtoMap.goodsUnitName1.message);
        }
        if(self.state.info.errorDtoMap.goodsUnitName2) {
            self.refs["goodsUnitName_2"].showErrorMsg(self.state.info.errorDtoMap.goodsUnitName2.message);
        }
        if(self.state.info.errorDtoMap.goodsUnitName3) {
            self.refs["goodsUnitName_3"].showErrorMsg(self.state.info.errorDtoMap.goodsUnitName3.message);
        }
        if(self.state.info.errorDtoMap.goodsUnitName4) {
            self.refs["goodsUnitName_4"].showErrorMsg(self.state.info.errorDtoMap.goodsUnitName4.message);
        }

        //声明单价
        if(self.state.info.errorDtoMap.goodsPrice0) {
            self.refs["goodsPrice_0"].showErrorMsg(self.state.info.errorDtoMap.goodsPrice0.message);
        }
        if(self.state.info.errorDtoMap.goodsPrice1) {
            self.refs["goodsPrice_1"].showErrorMsg(self.state.info.errorDtoMap.goodsPrice1.message);
        }
        if(self.state.info.errorDtoMap.goodsPrice2) {
            self.refs["goodsPrice_2"].showErrorMsg(self.state.info.errorDtoMap.goodsPrice2.message);
        }
        if(self.state.info.errorDtoMap.goodsPrice3) {
            self.refs["goodsPrice_3"].showErrorMsg(self.state.info.errorDtoMap.goodsPrice3.message);
        }
        if(self.state.info.errorDtoMap.goodsPrice4) {
            self.refs["goodsPrice_4"].showErrorMsg(self.state.info.errorDtoMap.goodsPrice4.message);
        }

        //描述
        if(self.state.info.errorDtoMap.description0) {
            self.refs["description_0"].showErrorMsg(self.state.info.errorDtoMap.description0.message);
        }
        if(self.state.info.errorDtoMap.description1) {
            self.refs["description_1"].showErrorMsg(self.state.info.errorDtoMap.description1.message);
        }
        if(self.state.info.errorDtoMap.description2) {
            self.refs["description_2"].showErrorMsg(self.state.info.errorDtoMap.description2.message);
        }
        if(self.state.info.errorDtoMap.description3) {
            self.refs["description_3"].showErrorMsg(self.state.info.errorDtoMap.description3.message);
        }
        if(self.state.info.errorDtoMap.description4) {
            self.refs["description_4"].showErrorMsg(self.state.info.errorDtoMap.description4.message);
        }

    },
    receiveMsg: function(data) {
        this.state.currency = data.currencyCode;
        this.setState();
    },
    getInitialState: function () {
        return this.props ? this.props : {};
    },

    initUnitSelect: function(){
        var self = this;
        for(var i = 0; i < this.state.data.length; i++){
            var selectUnit = "goodsUnitName_" + i;
            var unitSelectEle = "goodsUnitNameEle_" + i;
            (function(currentIndex){
                unitSelectEle = selectFactory({
                    data: {
                        'data' : self.state.info.unit
                    },
                    id: selectUnit,
                    defaultText: "请选择",
                    defaultCount: 1000,
                    isSaveInputVal: true,
                    isCreateNewSelect: true,
                    attrTextModel: function(name, data, item){

                        self.state.data[currentIndex]["goodsUnitName"] = item.name;
                        self.state.data[currentIndex]["goodsUnit"] = item.code;
                        self.setState();
                        self.refs["goodsUnitName_" + currentIndex].hideErrorMsg();
                    }
                });
            })(i);
        };
    },
    delGoods: function(index){
        var tempData = $.extend([],this.state.data);
        var self = this;


        if(tempData.length === 1){
            tempData.forEach(function(val){
                for(var i in val){
                    val[i] = "";
                }
                val["goodsUnitName"] = "件";
                val["goodsUnit"] = "pieces";
            });
            self.refs["goodsUnitName_0"].hideErrorMsg();
        }else{
            self.refs["goodsUnitName_" + index].hideErrorMsg();
            tempData.splice(index,1);

            //解决计量单位错误，删除中间物品，报错问题
            var unitArray=[];
            self.state.info.unit.forEach(function(val) {
                unitArray.push(val.name);
            });

            for(var j = index;j < tempData.length;j++){
                if(self.refs["goodsUnitName_"+j]){
                    if($.inArray(tempData[j].goodsUnitName,unitArray) < 0 ){
                        self.refs["goodsUnitName_"+j].showErrorMsg("物品计量单位不合法");
                    }else{
                        self.refs["goodsUnitName_"+j].hideErrorMsg();
                    }
                }
            }

        }
        this.setState({'data':tempData});
    },
    addGoods: function(){
        var tempArr = {
            'description': '',
            'goodsNameCn': '',
            'goodsNameEn': '',
            'goodsNumber': '',
            'goodsPrice': '',
            'goodsUnit': 'pieces',
            'goodsUnitName': '件',
            'hsCode': '',
            'id': ''
        };
        var tempData = $.extend([],this.state.data);
        tempData.push(tempArr);
        this.setState({'data':tempData},function(){
            this.initUnitSelect();
        });
    },
    setData: function(item, index, name) {
        var tempVal = $('#'+name+'_'+index).val();
        this.state.data[index][name] = tempVal;

    },
    changeHSLength: function(index){
        var hsVal = $('#hsCode_'+index).val();
        var rule = eval('(' +this.refs['hsCode_'+index].props.rule +')');
        if(hsVal.indexOf(".") !== -1 ){
            rule.maxLength = 11;
        }else{
            rule.maxLength = 10;
        }
        this.refs['hsCode_'+index].setMaxLength(JSON.stringify(rule));
    },
    getData: function(callback){
        var self = this, _arr = [], _subArr = [], isOk = true, resultData = [];
        if(self.state.data.length > 1){
            self.state.data.forEach(function(val, index){
                if(val.description !== "" || val.goodsNameEn !== "" || val.goodsNameCn !== "" || val.goodsNumber !== "" || val.goodsPrice !== "" || val.hsCode !== ""){
                    _arr.push(index);
                }
            });
            var keys = ["description", "goodsNameEn", "goodsNameCn", "goodsNumber", "goodsPrice"];
            if(_arr.length === 0){
                for(var m = 0; m < keys.length; m++){
                    if(!self.refs[keys[m]+'_0'].isOk()){
                        isOk = false;
                    }
                    if($("#hsCode_"+m).val() === "" || $("#hsCode_"+m).val() === null){
                        if(!self.refs["hsCode_0"].isOk()){
                            isOk = false;
                        }
                    }
                    if($("#goodsUnitName_"+m).val() === "" || $("#goodsUnitName_"+m).val() === null){
                        if(!self.refs["#goodsUnitName_"+m].isOk()){
                            isOk = false;
                        }
                    }

                }
            }else{
                for(var m = 0; m < keys.length; m++){
                    _arr.forEach(function(val, index){
                        if(!self.refs[keys[m]+'_'+val].isOk()){
                            isOk = false;
                        }
                        if($("#hsCode_"+val).val() === "" || $("#hsCode_"+val).val() === null){
                            if(!self.refs["hsCode_"+val].isOk()){
                                isOk = false;
                            }
                        }
                        if($("#goodsUnitName_"+val).val() === "" || $("#goodsUnitName_"+val).val() === null){
                            if(!self.refs["#goodsUnitName_"+val].isOk()){
                                isOk = false;
                            }
                        }
                    });
                }
            }
            if($(".error-msg").length > 0){
                isOk = false;
            }
            if(isOk){
                _arr.forEach(function(val, index){
                    resultData.push(self.state.data[val]);
                });
            }else{
                resultData = false;
            }
        }else{

            if($("#hsCode_0").val() === "" || $("#hsCode_0").val() === null){
                if(!self.refs["hsCode_0"].isOk()){
                    isOk = false;
                }
            }

            if($("#goodsUnitName_0").val() === "" || $("#goodsUnitName_0").val() === null){
                if(!self.refs["goodsUnitName_0"].isOk()){
                    isOk = false;
                }
            }

            for(var i in this.refs){
                if(i !== "hsCode_0" && i!= "goodsUnitName_0"){
                    if(!this.refs[i].isOk()){
                        isOk = false;
                    }
                }
            }
            if($(".error-msg").length > 0){
                isOk = false;
            }
            resultData = isOk ? self.state.data : false;
        }
        callback(resultData);
    },
    handleClick: function() {
        //eventFlux.dispatch(['index_slider_Event', 'index_init_Event'], {username: "xiaop"});
        //eventFlux.broadcast({username: "xiaop"});
    },
    render:function() {
        var data = this.state.data;
        var _this = this;
        var currency = this.state.currency;
        return ~goods;
    }
});
module.exports = GoodsComponent;